package com.cn.boot.student.service.student.impl;
import com.cn.boot.student.entity.WordEntity;
import com.cn.boot.student.mapper.WordMapper;
import com.cn.boot.student.service.student.IWordService;
import com.cn.boot.student.service.common.impl.RedisServiceImpl;
import com.cn.boot.student.util.CommonResult;
import com.cn.boot.student.util.Tool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
@CacheConfig(cacheNames = "words")
public class WordServiceImpl implements IWordService {
    @Autowired
    private WordMapper wordMapper;
    @Autowired
    private RedisServiceImpl redisService;

    /**
     * 查询单词（单词列表）
     * 条件查询
     *
     * @param wordEntity
     */
    @Override
    public CommonResult findAll(WordEntity wordEntity) {
        Integer count = wordMapper.getCount(wordEntity);
        PageHelper.startPage(Tool.toPageNum(wordEntity.getPageNum()), Tool.toPageSize(wordEntity.getPageSize()));
        List<WordEntity> list = wordMapper.findAll(wordEntity);
        if(list.size() == 0){
            return CommonResult.failed("当前暂无数据！");
        }
        PageInfo<WordEntity> pageInfo = new PageInfo<>(list);
        //返回收藏列表
        List<WordEntity> collectList = wordMapper.myCollection(wordEntity);
        //返回点赞列表
        List<WordEntity> likeList = wordMapper.myLike(wordEntity);
        HashMap<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        map.put("count",count);
        map.put("collectList",collectList);
        map.put("likeList",likeList);
        return CommonResult.success(map);
    }

    /**
     * 添加单词
     *
     * @param wordEntity
     */
    @Override
    public CommonResult add(WordEntity wordEntity) {
        Integer result = wordMapper.add(wordEntity);
        if(result == 0){
            return CommonResult.failed("单词添加失败！！！");
        }
        return CommonResult.success("单词添加成功！！！");
    }

    /**
     * 更新单词数据
     *
     * @param wordEntity
     */
    @Override
    public CommonResult update(WordEntity wordEntity) {
        Integer result = wordMapper.update(wordEntity);
        if(result == 0){
            return CommonResult.failed("单词更新失败！！！");
        }
        return CommonResult.success("单词更新成功！！！");
    }

    /**
     * 删除单词
     *
     * @param id
     */
    @Override
    public CommonResult del(Integer id) {
        Integer result = wordMapper.del(id);
        if(result == 0){
            return CommonResult.failed("单词删除失败！！！");
        }
        return CommonResult.success("单词删除成功！");
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public CommonResult dels(List<Integer> ids) {
        ids.stream().forEach(wordMapper::del);
        return CommonResult.success(ids.size(),"成功删除" + ids.size() + "条数据");
    }

    /**
     * 根据id查询单词
     *
     * @param id
     */
    @Override
    public CommonResult findById(Integer id) {
        WordEntity wordEntity = wordMapper.findById(id);
        return CommonResult.success(wordEntity);
    }

    /**
     * 单词收藏
     *
     * @param wordEntity
     */
    @Override
    public CommonResult collectWord(WordEntity wordEntity) {
        return null;
    }

    /**
     * 单词点赞
     *
     * @param wordEntity
     */
    @Override
    public CommonResult likeWord(WordEntity wordEntity) {
        return null;
    }

    /**
     * 我的单词列表
     *
     * @param wordEntity
     * @return
     */
    @Override
    public CommonResult myCollection(WordEntity wordEntity) {
        Integer count = wordMapper.getMyCollectCount(wordEntity);
        PageHelper.startPage(Tool.toPageNum(wordEntity.getPageNum()), Tool.toPageSize(wordEntity.getPageSize()));
        List<WordEntity> list = wordMapper.myCollection(wordEntity);
        if(list.size() == 0){
            return CommonResult.failed("当前暂无数据！");
        }
        PageInfo<WordEntity> pageInfo = new PageInfo<>(list);
        //返回点赞列表
        List<WordEntity> likeList = wordMapper.myLike(wordEntity);
        HashMap<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        map.put("count",count);
        map.put("likeList",likeList);
        return CommonResult.success(map);
    }
}
